home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 3006 / 3006.xpi / chrome / dwhelper.jar / content / dlproc-helper.js < prev    next >
Text File  |  2010-01-15  |  8KB  |  199 lines

  1. /******************************************************************************
  2.  *            Copyright (c) 2006-2009 Michel Gutierrez. All Rights Reserved.
  3.  ******************************************************************************/
  4.  
  5. /**
  6. * Object constructor
  7. */
  8. function DLProcHelper() {
  9.     try {
  10.         //dump("[DLProcHelper] constructor\n");
  11.         var prefService=Components.classes["@mozilla.org/preferences-service;1"]
  12.                                            .getService(Components.interfaces.nsIPrefService);
  13.         this.pref=prefService.getBranch("dwhelper.");
  14.         var prefService=Components.classes["@mozilla.org/preferences-service;1"]
  15.                                            .getService(Components.interfaces.nsIPrefService);
  16.         this.pref=prefService.getBranch("dwhelper.");
  17.         this.dlMgr=Components.classes["@downloadhelper.net/download-manager;1"]
  18.                                         .getService(Components.interfaces.dhIDownloadMgr);
  19.         this.cvMgr=Components.classes["@downloadhelper.net/convert-manager-component"]
  20.                                           .getService(Components.interfaces.dhIConvertMgr);
  21.         this.smartNamer = Components.classes["@downloadhelper.net/smart-namer;1"]
  22.                                         .getService(Components.interfaces.dhISmartNamer);
  23.     } catch(e) {
  24.         dump("[DLProcHelper] !!! constructor: "+e+"\n");
  25.     }
  26. }
  27.  
  28. DLProcHelper.prototype = {}
  29.  
  30. DLProcHelper.prototype.canHandle=function(desc) {
  31.     //dump("[DLProcHelper] canHandle()\n");
  32.     if(desc.has("media-url") || desc.has("links"))
  33.         return true;
  34.     else
  35.         return false;
  36. }
  37.  
  38. DLProcHelper.prototype.requireDownload=function(desc) {
  39.     //dump("[DLProcHelper] requireDownload()\n");
  40.     return desc.has("media-url");
  41. }
  42.  
  43. DLProcHelper.prototype.preDownload=function(desc,promptDownload,promptConversion) {
  44.     try {
  45.         //dump("[DLProcHelper] preDownload()\n");
  46.         var filename=Util.getPropsString(desc,"file-name");
  47.         if(filename==null)
  48.             filename="dwhelper-video.flv";
  49.         if(desc.has("format"))
  50.             desc.undefine("format");
  51.         var format=this.cvMgr.getFormat(filename,Util.getPropsString(desc,"media-url"),Util.getPropsString(desc,"page-url"));
  52.         if(promptConversion) {
  53.             var windowMediator = Components.classes["@mozilla.org/appshell/window-mediator;1"]
  54.                                                     .getService(Components.interfaces.nsIWindowMediator);
  55.                var window = windowMediator.getMostRecentWindow("navigator:browser");
  56.             var data={
  57.                     format: format
  58.             };
  59.             window.openDialog("chrome://dwhelper/content/convert-manual.xul",
  60.                          "dwhelper-convert-manual", "chrome,centerscreen,modal",data);
  61.             format=data.format;
  62.         } 
  63.         if(format) {
  64.             Util.setPropsString(desc,"format",format);
  65.             var ext=/^(.*?)\//.exec(format)[1];
  66.             filename=/^(.*?)(?:\.[^\.]{1,5})?$/.exec(filename)[1]+"."+ext;
  67.             if(desc.has("dl-file"))
  68.                 desc.undefine("dl-file");
  69.         }
  70.         var file=this.dlMgr.getDownloadDirectory();
  71.         file.append(filename);
  72.         try {
  73.             file.createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 0644);
  74.         } catch(e) {
  75.             Util.alertError(Util.getFText("error.cannot-create-target-file",[file.path],1));
  76.             return false;
  77.         }
  78.  
  79.          if(promptDownload) {
  80.              file.remove(true);
  81.              
  82.             var windowMediator = Components.classes["@mozilla.org/appshell/window-mediator;1"]
  83.                                         .getService(Components.interfaces.nsIWindowMediator);
  84.             var window = windowMediator.getMostRecentWindow("navigator:browser");
  85.             var saveFilePicker=Components.classes["@mozilla.org/filepicker;1"]
  86.                                                   .createInstance(Components.interfaces.nsIFilePicker);
  87.             saveFilePicker.init(window, Util.getText("title.save-file"), 
  88.                     Components.interfaces.nsIFilePicker.modeSave);
  89.             saveFilePicker.displayDirectory=file.parent;
  90.             saveFilePicker.defaultString=file.leafName;
  91.             saveFilePicker.appendFilters(Components.interfaces.nsIFilePicker.filterAll);
  92.              var orgLeafName=file.leafName;
  93.             var rs=saveFilePicker.show();
  94.             if(rs==Components.interfaces.nsIFilePicker.returnCancel) {
  95.                 return false;
  96.             }
  97.             file=saveFilePicker.file;
  98.             if(file.leafName==orgLeafName)
  99.                 this.smartNamer.incrNameStat(desc,"keep");
  100.             else
  101.                 this.smartNamer.incrNameStat(desc,"nkeep");
  102.             this.dlMgr.setDownloadDirectory(file.parent);
  103.          }
  104.  
  105.         if(format) {
  106.             if(this.cvMgr.checkConverter(true)==false) {
  107.                 if(file.exists())
  108.                     file.remove(true);
  109.                 return false;
  110.             }
  111.             desc.set("cv-file",file);
  112.          } else {
  113.              desc.set("dl-file",file);
  114.          }
  115.         
  116.          return true;
  117.     } catch(e) {
  118.         dump("!!! [DLProcHelper] preDownload(): "+e+"\n");
  119.         return false;
  120.     }
  121. }
  122.  
  123. DLProcHelper.prototype.handle=function(desc,promptDownload) {
  124.     //dump("[DLProcHelper] handle()\n");
  125.     try {
  126.         if(!desc.has("media-url")) {
  127.             var dir=this.dlMgr.getDownloadDirectory();
  128.             var dirFileName=Util.getPropsString(desc,"file-name");
  129.             if(dirFileName && dirFileName.length>0)
  130.                 dir.append(dirFileName);
  131.             else
  132.                 dir.append("medialink");
  133.              dir.createUnique(Components.interfaces.nsIFile.DIRECTORY_TYPE, 0755);
  134.              
  135.              if(promptDownload) {
  136.                  dir.remove(true);
  137.     
  138.                 var windowMediator = Components.classes["@mozilla.org/appshell/window-mediator;1"]
  139.                                                         .getService(Components.interfaces.nsIWindowMediator);
  140.                 var window = windowMediator.getMostRecentWindow("navigator:browser");
  141.                 var saveFilePicker=Components.classes["@mozilla.org/filepicker;1"]
  142.                                                       .createInstance(Components.interfaces.nsIFilePicker);
  143.                 saveFilePicker.init(window, Util.getText("title.save-file"), 
  144.                         Components.interfaces.nsIFilePicker.modeSave);
  145.                 saveFilePicker.displayDirectory=dir.parent;
  146.                 saveFilePicker.defaultString=dir.leafName;
  147.                 saveFilePicker.appendFilters(Components.interfaces.nsIFilePicker.filterAll);
  148.     
  149.                 var rs=saveFilePicker.show();
  150.                 if(rs==Components.interfaces.nsIFilePicker.returnCancel) {
  151.                     return;
  152.                 }
  153.                 var dir=saveFilePicker.file;
  154.                  dir.createUnique(Components.interfaces.nsIFile.DIRECTORY_TYPE, 0755);
  155.                 this.dlMgr.setDownloadDirectory(dir.parent);
  156.              }
  157.              
  158.              var links=desc.get("links",Components.interfaces.nsIArray);
  159.  
  160.              var doIndexPrefix=true;
  161.             var indexPrefix=0;
  162.             try {
  163.                 doIndexPrefix=this.pref.getBoolPref("medialink-index-prefix");
  164.             } catch(e) {}
  165.             if(links.length<2)
  166.                 doIndexPrefix=false;
  167.  
  168.              var i=links.enumerate();
  169.              while(i.hasMoreElements()) {
  170.                  var entry=i.getNext().QueryInterface(Components.interfaces.nsIProperties);
  171.                  var mediaUrl=Util.getPropsString(entry,"media-url");
  172.                 var fileName=/.*\/(.*?)$/.exec(mediaUrl)[1];
  173.                 if(doIndexPrefix) {
  174.                     indexPrefix++;
  175.                     var prefix="0000".substring(0,4-(""+indexPrefix).length)+indexPrefix;
  176.                     fileName=prefix+"-"+fileName;
  177.                 }
  178.                 var file=dir.clone();
  179.                 file.append(fileName);
  180.                 entry.set("dl-file",file);
  181.                 Util.setPropsString(entry,"label",fileName);
  182.                 this.dlMgr.download(null,entry,null);
  183.              }
  184.         }
  185.         if(promptDownload==false && this.pref.getBoolPref("quickdownload-notify")) {
  186.             try {
  187.                 var alertsService = Components.classes["@mozilla.org/alerts-service;1"]
  188.                                                        .getService(Components.interfaces.nsIAlertsService);
  189.                 var title=Util.getText("notification.quickdownload-requested.title");
  190.                 var message=Util.getFText("notification.quickdownload-requested",[Util.getPropsString(desc,"file-name")],1);
  191.                 var iconUrl="chrome://dwhelper/skin/dwhelper24-on.gif";
  192.                 alertsService.showAlertNotification(iconUrl,title,message);
  193.             } catch(e) {}
  194.         }
  195.     } catch(e) {
  196.         dump("!!! [QDLProcHelper] handle(): "+e+"\n");
  197.     }
  198. }
  199.